package com.siashan.toolkit.sms.baiducloud.core;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV3Request;
import com.baidubce.services.sms.model.SendMessageV3Response;
import com.siashan.toolkit.sms.core.entity.*;
import com.siashan.toolkit.sms.core.executor.AbstractSmsSendExecutor;
import com.siashan.toolkit.sms.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 阿里云短信通知执行器
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class BaiduCloudSmsSendExecutor extends AbstractSmsSendExecutor {

    /**
     * 短信客户端
     */
    private final SmsClient client;


    /**
     * 阿里云短信配置
     */
    private BaiduCloudSmsConfig baiduCloudSmsConfig;

    /**
     * 构造百度云短信发送处理
     *
     * @param config 百度云短信配置
     */
    public BaiduCloudSmsSendExecutor(BaiduCloudSmsConfig config) {
        super(config);
        this.baiduCloudSmsConfig = config;
        SmsClientConfiguration smsClientConfiguration = new SmsClientConfiguration();
        smsClientConfiguration.setCredentials(new DefaultBceCredentials(config.getAccessKeyId(), config.getSecretAccessKey()));
        config.setEndpoint(config.getEndpoint());
        client = new SmsClient(smsClientConfiguration);
    }
    /**
     * 构造百度云短信发送处理
     *
     * @param config 百度云短信配置
     */
    public BaiduCloudSmsSendExecutor(BaiduCloudSmsConfig config, Map<String, SmsTemplate> templateMap) {
        this(config);
        this.templateMap = templateMap;
    }

    /**
     * 构造百度云短信发送处理
     *
     * @param config 百度云短信配置
     */
    public BaiduCloudSmsSendExecutor(BaiduCloudSmsConfig config, Map<String, SmsTemplate> templateMap,SmsWhiteList whiteList) {
        this(config,templateMap);
        this.whiteList = whiteList;
    }

    /**
     * 构造百度云短信发送处理
     *
     * @param config 百度云短信配置
     */
    public BaiduCloudSmsSendExecutor(BaiduCloudSmsConfig config, Map<String, SmsTemplate> templateMap,SmsWhiteList whiteList,SmsFrequencyLimitConfig frequencyLimit) {
        this(config,templateMap,whiteList);
        this.frequencyLimit = frequencyLimit;
    }


    /**
     * 发送批量通知
     *
     * @param smsSendModel 通知参数
     * @return 是否发送成功
     */
    @Override
    public boolean doSend(SmsModel smsSendModel) {
        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(StringUtils.join(smsSendModel.getPhones(), ","));
        request.setSignatureId(baiduCloudSmsConfig.getSignatureId());
        request.setTemplate(smsSendModel.getTemplateId());
        request.setContentVar(smsSendModel.getParams());

        SendMessageV3Response response = client.sendMessage(request);
        if (null != response && response.isSuccess()) {
            return true;
        }
        log.warn("sms send fail[code={}, message={}]", response.getCode(), response.getMessage());
        return false;
    }


    /**
     * 获取通道
     *
     * @return 通道
     */
    @Override
    public SmsChannel getChannel() {
        return SmsChannel.BAIDU;
    }



}
